/**
* This is about <code>org.shoebox.guacamole.materials.TextureRegion</code>.
* {@link www.hyperfiction.fr}
* @author shoe[box]
*/

package org.shoebox.guacamole.materials {

	import com.adobe.utils.AGALMiniAssembler;
	import org.shoebox.guacamole.materials.abstracts.AMaterial;
	import org.shoebox.guacamole.view.abstracts.GuaMesh;
	import org.shoebox.utils.logger.Logger;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.Program3D;
	import flash.display3D.textures.Texture;
	
	/**
	* org.shoebox.guacamole.materials.TextureRegion
	* @author shoebox
	*/
	public class TextureRegion extends AMaterial{
		
		public var refAtlas		: AtlasMaterial;
		public var dx				: Number;
		public var dy 				: Number;
		public var width			: Number;
		public var height			: Number;
		
		protected var _vCoordinates		: Vector.<Number>;
		
		// -------o constructor
		
			/**
			* Constructor of the TextureRegion class
			*
			* @public
			* @return	void
			*/
			public function TextureRegion( sName : String , refAtlas : AtlasMaterial = null ) : void {
				super( sName );
				this.refAtlas = refAtlas;
			}

		// -------o public
			
			/**
			* getVector function
			* @public
			* @param 
			* @return
			*/
			final public function getCoordsVec() : Vector.<Number> {
				
				if( !_vCoordinates ){
					
					_vCoordinates = Vector.<Number>( [ 
				
															dx			, dy, 			// 0 
															dx + width	, dy,			// 1 
															dx			, dy + height,	// 3 
															dx + width	, dy + height	// 2
														] );
				}
				return _vCoordinates;
			}
			
			/**
			* set matId function
			* @public
			* @param 
			* @return
			*/
			final override public function setMatId( id : int ) : void {
			
				refAtlas.setMatId( id );
								
			}
			
			/**
			* preprod function
			* @public
			* @param 
			* @return
			*/
			override public function preprod( oMesh : GuaMesh ) : void {
				oMesh.setUvs( getCoordsVec( ) , Context3DVertexBufferFormat.FLOAT_2 );
				refAtlas.preprod( oMesh );
			}
			
			/**
			* postProd function
			* @public
			* @param 
			* @return
			*/
			override public function postprod( mesh : GuaMesh ) : void {
				refAtlas.postprod( mesh );
			}
			
			/**
			* getMaterial function
			* @public
			* @param 
			* @return
			*/
			final override public function getProgram( vertex : AGALMiniAssembler ) : Program3D {
				return refAtlas.getProgram( vertex );
			}
			
			/**
			* getTexture function
			* @public
			* @param 
			* @return
			*/
			final public function getTexture() : Texture {
				return refAtlas.tTexture;
			}
			
		// -------o protected

		// -------o misc

			public static function trc(...args : *) : void {
				Logger.log(TextureRegion, args);
			}
	}
}
